package com.ldw.demo.controller;

import com.ldw.demo.pojo.Role;
import com.ldw.demo.pojo.User;
import com.ldw.demo.service.UserService;
import com.ldw.demo.utils.Constans;
import com.ldw.demo.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired(required = false)
    private UserService userService;


    @PostMapping("/login.do")
    public String queryUserByName(@RequestParam("userCode") String userCode, @RequestParam("userPassword") String userPassword, Model model, HttpSession session) {
        User user = userService.queryUserByName(userCode, userPassword);
        if (user != null) {
            session.setAttribute("userInfo", user);
            return "frame";
        }
        model.addAttribute("error", "用户名或密码错误！！！");
        return "login";
    }

    @GetMapping("/jsp/logout.do")
    public String logout(HttpSession session) {
        session.removeAttribute("userInfo");
        return "login";
    }

    @GetMapping("/jsp/userlist.do")
    public String userList(Model model, @Nullable @RequestParam("queryname") String queryname,
                           @Nullable @RequestParam("queryUserRole") String queryUserRole,
                           @Nullable @RequestParam("pageIndex") String pageIndex

    ) {
        HashMap map = new HashMap();
        List<Role> roleList = userService.queryRole();

        PageUtil page = new PageUtil();
        page.setPageSize(Constans.PAGESIZE);
        page.setCurrentPage(pageIndex);
        if(queryUserRole != null && Integer.parseInt(queryUserRole) == 0) {
            queryUserRole = null;
        }
        int currentPageNo = page.getCurrentPage();
        int pageSize = page.getPageSize();
        int index = currentPageNo * pageSize - pageSize;
        map.put("queryname", queryname);
        map.put("queryUserRole", queryUserRole);
        map.put("currentPageNo", index);
        map.put("pageSize", pageSize);
        int userCount = userService.getUserCount(map);
        List<User> userList = userService.queryUser(map);
        page.setPageCount(userCount);
        model.addAttribute("userList", userList);
        model.addAttribute("roleList", roleList);
        model.addAttribute("totalPageCount", page.getSumPage());
        model.addAttribute("totalCount", page.getPageCount());
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("queryUserName", queryname);
        model.addAttribute("queryUserRole", queryUserRole);
        return "userlist";
    }

    @GetMapping("/jsp/user.do")
    @ResponseBody
    public Map pwdModfiy(@RequestParam("method") String method,@Nullable @RequestParam("oldpassword") String oldpassword,
                         @Nullable @RequestParam("userCode") String userCode, HttpSession session) {
        HashMap<String, String> result = new HashMap<>();
        if("pwdmodify".equals(method)) {
            User userInfo = (User) session.getAttribute("userInfo");
            String userPassword = userInfo.getUserPassword();
            if (userPassword == null) {
                result.put("result", "sessionerror");
            } else if (oldpassword == null) {
                result.put("result", "error");
            } else {
                if (userPassword.equals(oldpassword)) {
                    result.put("result", "true");
                } else {
                    result.put("result", "false");
                }
            }
        }
        else if("ucexist".equals(method)) {
            User user = userService.querUserCodeByName(userCode);
            if(user != null) {
                result.put("userCode", "exist");
            }
        }
        else if("getrolelist".equals(method)) {
            List<Role> roleList = userService.queryRole();

        }
        return result;
    }

    
    @PostMapping("/jsp/user.do")
    public String savePwd(@RequestParam("newpassword") String newpassword, HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        int uid = userInfo.getId();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("newpassword", newpassword);
        map.put("uid", uid);
        Boolean flag = userService.pwdModity(map);
        if (flag) {
            return "login";
        }
        return "error";
    }
}
